﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Modeling_Sub_Pages_NewsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            if (Session["WelcomeUser"] != null)
            {
                if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("EAS"))
                {
                    UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

                    //Lay id truyen tu querry string
                    int id = Convert.ToInt32(Request.QueryString["id"].ToString());

                    bool end = Convert.ToBoolean(udm.showCoursesByID(udm.createConnection(), id).Tables[0].Rows[0][20].ToString());

                    if (end)
                    {
                        Response.Redirect("MaintainMessage.aspx?ID=12");
                    }

                    // Ko chay lai doan nay khi postback
                    if (!IsPostBack)
                    {
                        ddlTeacher.DataSource = udm.showTeacher(udm.createConnection());
                        ddlTeacher.DataTextField = "Full Name";
                        ddlTeacher.DataValueField = "ID";
                        ddlTeacher.DataBind();

                        ddlSubject.DataSource = udm.showSubject(udm.createConnection());
                        ddlSubject.DataTextField = "Name";
                        ddlSubject.DataValueField = "ID";
                        ddlSubject.DataBind();

                        // Load thong tin event co id tren vao form
                        DataSet ds = new DataSet();
                        ds = udm.showCoursesByID(udm.createConnection(), id);

                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            string name = ds.Tables[0].Rows[0][1].ToString();
                            string subject = ds.Tables[0].Rows[0][2].ToString();
                            int teacher = Convert.ToInt32(ds.Tables[0].Rows[0][4].ToString());
                            DateTime dateStart = Convert.ToDateTime(ds.Tables[0].Rows[0][6].ToString());
                            bool monday = Convert.ToBoolean(ds.Tables[0].Rows[0][7].ToString());
                            bool tuesday = Convert.ToBoolean(ds.Tables[0].Rows[0][8].ToString());
                            bool wednesday = Convert.ToBoolean(ds.Tables[0].Rows[0][9].ToString());
                            bool thursday = Convert.ToBoolean(ds.Tables[0].Rows[0][10].ToString());
                            bool friday = Convert.ToBoolean(ds.Tables[0].Rows[0][11].ToString());
                            bool saturday = Convert.ToBoolean(ds.Tables[0].Rows[0][12].ToString());
                            bool sunday = Convert.ToBoolean(ds.Tables[0].Rows[0][13].ToString());
                            DateTime timeStart = Convert.ToDateTime(ds.Tables[0].Rows[0][14].ToString());
                            DateTime timeEnd = Convert.ToDateTime(ds.Tables[0].Rows[0][15].ToString());
                            string location = ds.Tables[0].Rows[0][16].ToString();
                            string description = ds.Tables[0].Rows[0][18].ToString();

                            // Bat dau do vao form
                            txtTitle.Text = name;
                            ddlSubject.SelectedValue = subject;
                            ddlTeacher.SelectedValue = teacher.ToString();
                            Calendar1.SelectedDate = dateStart;
                            txtDateTime.Text = dateStart.ToShortDateString();
                            cbMonday.Checked = monday;
                            cbTuesday.Checked = tuesday;
                            cbWednesday.Checked = wednesday;
                            cbThursday.Checked = thursday;
                            cbFriday.Checked = friday;
                            cbSaturday.Checked = saturday;
                            cbSunday.Checked = sunday;
                            txtHour.Text = timeStart.Hour.ToString();
                            txtMinute.Text = timeStart.Minute.ToString();
                            txtHourEnd.Text = timeEnd.Hour.ToString();
                            txtMinuteEnd.Text = timeEnd.Minute.ToString();
                            txtLocation.Text = location;
                            txtDescription.Text = description;
                        }
                    }
                }
                else
                {
                    Response.Redirect("~/LoginMessage.aspx?id=1");
                }
            }
            else
            {
                Response.Redirect("~/Login.aspx?id=2");
            }
        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Calendar1.Visible = true;
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        DateTime date = Calendar1.SelectedDate;
        txtDateTime.Text = date.ToString("dd-MM-yyyy");
        Calendar1.Visible = false;
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtTitle.Text = "";
        txtDateTime.Text = "";
        txtLocation.Text = "";
        txtDescription.Text = "";
        txtHour.Text = "";
        txtMinute.Text = "";
        txtHourEnd.Text = "";
        txtMinuteEnd.Text = "";
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();
        
        string name = txtTitle.Text;
        string subjectID = ddlSubject.SelectedValue.ToString();
        int teacherID = Convert.ToInt32(ddlTeacher.SelectedValue.ToString());
        string location = txtLocation.Text;
        string content = txtDescription.Text;

        int hour = Convert.ToInt32(txtHour.Text);
        int minute = Convert.ToInt32(txtMinute.Text);

        DateTime datestart = new DateTime(Calendar1.SelectedDate.Year, Calendar1.SelectedDate.Month, Calendar1.SelectedDate.Day, hour, minute, 0);

        int hourEnd = Convert.ToInt32(txtHourEnd.Text);
        int minuteEnd = Convert.ToInt32(txtMinuteEnd.Text);

        DateTime dateEnd = new DateTime(Calendar1.SelectedDate.Year, Calendar1.SelectedDate.Month, Calendar1.SelectedDate.Day, hourEnd, minuteEnd, 0);

        bool monday = cbMonday.Checked;
        bool tuesday = cbTuesday.Checked;
        bool wednesday = cbWednesday.Checked;
        bool thursday = cbThursday.Checked;
        bool friday = cbFriday.Checked;
        bool saturday = cbSaturday.Checked;
        bool sunday = cbSunday.Checked;

        int id = Convert.ToInt32(Request.QueryString["id"].ToString());

        if (DateTime.Compare(DateTime.Today, datestart) <= 0)
        {
            if (hourEnd > hour)
            {
                if (udm.updateCourse(udm.createConnection(),id, name, subjectID, teacherID, datestart, location, content))
                {
                    if (udm.updateTimeTable(udm.createConnection(), id, monday, tuesday, wednesday, thursday, friday, saturday, sunday, datestart, dateEnd))
                    {
                        Response.Redirect("MaintainMessage.aspx?ID=13");
                    }
                    else
                    {
                        Response.Redirect("MaintainMessage.aspx?ID=14");
                    }
                }
                else {
                    Response.Redirect("MaintainMessage.aspx?ID=14");
                }
            }
        }     
    }
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        DateTime datestart = Calendar1.SelectedDate;

        if (DateTime.Compare(DateTime.Today, datestart) <= 0)
        {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }

    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        int hourStart = Convert.ToInt32(txtHour.Text);
        int hourEnd = Convert.ToInt32(txtHourEnd.Text);

        if (hourEnd > hourStart)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
}